package com.yz.xyt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.Constants;
import com.yz.xyt.domain.Notice;
import com.yz.xyt.domain.vo.NoticeVo;
import com.yz.xyt.mapper.NoticeMapper;
import com.yz.xyt.service.INoticeService;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通知公告service
 * @author JW.WANG
 */
@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Notice record) {
        return noticeMapper.insert(record);
    }

    @Override
    public int insertSelective(Notice record) {
        return noticeMapper.insertSelective(record);
    }

    @Override
    public Notice selectByPrimaryKey(Integer id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Notice record) {
        return noticeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Notice record) {
        return noticeMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(Notice record) {
        return noticeMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Notice> findNoticeList(Notice record) {
        return noticeMapper.findNoticeList(record);
    }

    /**
     * 分页查询
     * @param noticeVo
     * @return
     */
    @Override
    public PageInfo findForPage(NoticeVo noticeVo) {

        PageHelper.startPage(noticeVo.getPageNumber(),noticeVo.getPageSize());
        //默认情况下app用户获取已发布的通知公告
        boolean appUser = SecurityUtils.getLoginUser().isAppUser();
        if(appUser){
            noticeVo.setStatus(Constants.NOTICE_PUBLISH);
        }
        List<Notice> noticeList = findNoticeList(noticeVo);
        noticeList.stream().forEach(notice -> {
            String status = notice.getStatus();
            if(Constants.NOTICE_NO_PUBLISH.equals(status)){
                notice.setStatus(Constants.TRANSLATE_NOTICE_NO_PUBLISH);
            }
            if(Constants.NOTICE_OFF.equals(status)){
                notice.setStatus(Constants.TRANSLATE_NOTICE_OFF);
            }
            if(Constants.NOTICE_PUBLISH.equals(status)){
                notice.setStatus(Constants.TRANSLATE_NOTICE_PUBLISH);
            }
        });
        return new PageInfo(noticeList);
    }
}
